package org.jiuling.basics.system;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * 系统信息
 * @author 紫灵
 * @date 2025年09月26日 09时38分39秒
 */
public class SystemInfo {

    /**
     * 获取系统名称
     * @return 系统名称
     */
    public static String getOsName(){
        return System.getProperty("os.name").toLowerCase();
    }

    /**
     * 获取CPU核数
     * @return CPU核数
     */
    public static int getCPUCoreCount(){
        return Runtime.getRuntime().availableProcessors();
    }

    /**
     * 获取内存大小
     * @return 内存大小
     */
    public static long getMemorySize(){
        return Runtime.getRuntime().totalMemory();
    }

    /**
     * 获取最大内存大小
     * @return 最大内存大小
     */
    public static long getMaxMemorySize(){
        return Runtime.getRuntime().maxMemory();
    }

    /**
     * 获取空闲内存大小
     * @return 空闲内存大小
     */
    public static long getFreeMemorySize(){
        return Runtime.getRuntime().freeMemory();
    }

    /**
     * 获取MAC地址
     * @return MAC地址
     */
    public static String getMacAddress() {
        InetAddress ip;
        try {
            ip = InetAddress.getLocalHost();
            NetworkInterface network = NetworkInterface.getByInetAddress(ip);
            byte[] mac = network.getHardwareAddress();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
            }
            return sb.toString();
        } catch (UnknownHostException | SocketException e) {
            return "";
        }
    }

//    public static final String OS = System.getProperty("os.name").toLowerCase();
//    public static final String JAVA_VERSION = System.getProperty("java.version");
//    public static final String JAVA_VENDOR = System.getProperty("java.vendor");
//    public static final String JAVA_HOME = System.getProperty("java.home");
//    public static final String JAVA_CLASS_PATH = System.getProperty("java.class.path");
//    public static final String JAVA_LIBRARY_PATH = System.getProperty("java.library.path");

    public static void main(String[] args) {
        System.out.println("CPU核数：" + Runtime.getRuntime().availableProcessors());
        System.out.println("JVM总内存：" + Runtime.getRuntime().totalMemory() / (1024 * 1024) + "M");
        System.out.println("JVM空闲内存：" + Runtime.getRuntime().freeMemory() / (1024 * 1024) + "M");
        System.out.println("JVM可用内存：" + Runtime.getRuntime().maxMemory() / (1024 * 1024) + "M");
    }


}
